django.db.utils.OperationalError:外鍵不匹配 - “project_projectpage”引用“auth_user” (django.db.utils.OperationalError: foreign key mismatch - "project_projectpage" referencing "auth_user")


問題描述

django.db.utils.OperationalError:外鍵不匹配 ‑ “project_projectpage”引用“auth_user” (django.db.utils.OperationalError: foreign key mismatch ‑ "project_projectpage" referencing "auth_user")

這個問題首先想說什麼?

下面是我的model.py

class ProjectPage(Page):
"""
A Project Page

We access the People object with an inline panel that references the
ParentalKey's related_name in ProjectPeopleRelationship. More docs:
http://docs.wagtail.io/en/latest/topics/pages.html#inline‑models
"""
introduction = models.TextField(
    help_text='Text to describe the page',
    blank=True)
image = models.ForeignKey(
    'wagtailimages.Image',
    null=True,
    blank=True,
    on_delete=models.SET_NULL,
    related_name='+',
    help_text='Landscape mode only; horizontal width between 1000px and 3000px.'
)
body = StreamField(
    BaseStreamBlock(), verbose_name="Page body", blank=True
)
subtitle = models.CharField(blank=True, max_length=255)
tags = ClusterTaggableManager(through=ProjectPageTag, blank=True)
date_published = models.DateField(
    "Date article published", blank=True, null=True
    )
#email = models.CharField(max_length=255, blank=True, null=True)
email = models.ForeignKey(User, on_delete=models.PROTECT, to_field='email', null=True)

content_panels = Page.content_panels + [
    FieldPanel('subtitle', classname="full"),
    FieldPanel('introduction', classname="full"),
    ImageChooserPanel('image'),
    StreamFieldPanel('body'),
    FieldPanel('date_published'),
    InlinePanel(
        'project_person_relationship', label="Author(s)",
        panels=None, min_num=1),
    FieldPanel('email'),
    FieldPanel('tags'),
    FieldPanel('added_by', classname="full"),
    FieldPanel('updated_by', classname="full"),
]

search_fields = Page.search_fields + [
    index.SearchField('body'),
]
added_by = models.ForeignKey(People,related_name="project_added_by", null=True, blank=True, on_delete=models.SET_NULL)
updated_by = models.ForeignKey(People,related_name="project_updated_by", null=True, blank=True, on_delete=models.SET_NULL)

created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True)
updated_at = models.DateTimeField(auto_now=True, null=True, blank=True)

上述問題的解決方案是什麼,如果您想了解更多詳細信息,請告訴我對於這個問題!!!!

我不知道,但我認為這裡的電子郵件 ID 或其他地方有問題,因為我添加了電子郵件 ID,之後可能會出現這個問題。


參考解法

方法 1:

The declaration for email on Django's User model is:

email = models.EmailField(_('email address'), blank=True)

This part of the Django documentation says that, if you link to any field besides the default key (such as how you're trying to link to email), then that field would have to be declared with unique=True, but email isn't. The problem with how you're trying to link is that there is no index on the User model's email field because it isn't declared as unique.

It would be more standard to link to the Django user model like this (by default, the link is made on the user model's default key, the id):

from django.contrib.auth import get_user_model

user = models.ForeignKey(get_user_model(), on_delete=models.PROTECT, null=True)

With the above declaration, in your template you would get the linked user's email by {{ page.user.email }}, but I wonder about the architecture of linking a user to a page. Remember that Wagtail has a permissions structure that you can use to assign permissions to groups of users for specific page types/models.

(by Chirag KanzariyaDan Swain)

參考文件

  1. django.db.utils.OperationalError: foreign key mismatch ‑ "project_projectpage" referencing "auth_user" (CC BY‑SA 2.5/3.0/4.0)

#wagtail #Python #Django






相關問題

Wagtail Cms 是否支持 Google 登錄和用戶登錄添加會話 (Does Wagtail Cms support Google login and user login add session to)

Wagtail Django-form編輯現有對象 (Wagtail Django-form edit existing object)

如何將 Wagtail 'admin' 菜單添加到自定義模板? (How to add Wagtail 'admin' menu to custom templates?)

django.db.utils.OperationalError:外鍵不匹配 - “project_projectpage”引用“auth_user” (django.db.utils.OperationalError: foreign key mismatch - "project_projectpage" referencing "auth_user")

如何使用 Wagtail 鉤子在 Wagtail 中生成自定義鏈接 (How to generate a custom link in Wagtail using Wagtail hooks)

Wagtail:如何設置單元測試以進行簡單的頁面編輯? (Wagtail: How to setup up unittest for simple page edit?)

如何修復錯誤“str”對像沒有屬性“relative_url” (How to fix error 'str' object has no attribute 'relative_url')

如何將帖子從 Wordpress 導入 Wagtail 2(Draftail 編輯器),包括圖像? (How to import posts from Wordpress to Wagtail 2 (Draftail editor) including images?)

如何用外鍵鏈接兩種形式(wagtail 形式和 django 形式)? (How to link two forms (wagtail form and django form) with a foreign key?)

為什麼 RichText 不能在 wagtail 管理員中為帖子工作?這是發生的事情的類型:<h2>嘗試 post.content|richtext</h2> (Why is RichText not working in wagtail admin for posts? This is the type of thing that happens: <h2>Trying post.content|richtext</h2>)

Windows 10 上 wagtail 的客戶端文件夾在哪裡 (Where is the client folder of wagtail on windows 10)

過濾從 Wagtail 核心頁面導入的多個模型的自定義字段 (Filter on custom field across multiple models that import from Wagtail core Page)







留言討論